package com.terrace.core.mybatis.plugin.pagination.parser;

import java.util.Map;

import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;

import com.terrace.core.mybatis.entity.Pagination;

/**
 * @author liuzh
 */
public class MysqlParser extends AbstractParser {
	
	@Override
	public String getPaginationSQL(String sql) {
		StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14);
		sqlBuilder.append("SELECT * FROM (");
        sqlBuilder.append(sql);
        sqlBuilder.append(") _SELECT_PAGINATION LIMIT ?,?");
        return sqlBuilder.toString();
	}
	
    @Override
    public Map<String, Object> setPageParameter(MappedStatement ms, Object parameterObject, BoundSql boundSql, Pagination<?> page) {
        Map<String, Object> paramMap = super.setPageParameter(ms, parameterObject, boundSql, page);
        paramMap.put(PAGEPARAMETER_FIRST, page.getStartRow());
        paramMap.put(PAGEPARAMETER_SECOND, page.getRows());
        return paramMap;
    }

}